home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-04-21 | 3.8 KB | 156 lines |
- package com.symantec.itools.beans;
-
- import java.beans.*;
-
- public class TaggedIntPropertyEditor
- extends PropertyEditorSupport
- {
- static protected class TaggedInt
- {
- public TaggedInt(String tag, int value, String initializationString)
- {
- m_Tag = tag;
- m_Value = value;
- m_InitializationString = initializationString;
- }
-
- public String getTag()
- {
- return m_Tag;
- }
-
- public int getValue()
- {
- return m_Value;
- }
-
- public String getInitializationString()
- {
- return m_InitializationString;
- }
-
- protected String m_Tag;
- protected int m_Value;
- protected String m_InitializationString;
- }
-
- private TaggedIntPropertyEditor()
- {
- }
-
- public TaggedIntPropertyEditor(TaggedInt[] tags)
- {
- this.m_Tags = tags;
- }
-
- /**
- * If the property value must be one of a set of known tagged values,
- * then this method should return an array of the tag values. This can
- * be used to represent (for example) enum values. If a PropertyEditor
- * supports tags, then it should support the use of setAsText with
- * a tag value as a way of setting the value.
- *
- * @return The tag values for this property. May be null if this
- * property cannot be represented as a tagged value.
- *
- */
- public String[] getTags()
- {
- String[] tags = new String[m_Tags.length];
-
- for (int i = 0;i < m_Tags.length;i++)
- tags[i] = m_Tags[i].getTag();
-
- return tags;
- }
-
- /**
- * @return The property value as a string suitable for presentation
- * to a human to edit.
- * <p> Returns "null" is the value can't be expressed as a string.
- * <p> If a non-null value is returned, then the PropertyEditor should
- * be prepared to parse that string back in setAsText().
- */
- public String getAsText()
- {
- return getValueAsString(true);
- }
-
- /**
- * Set the property value by parsing a given String. May raise
- * java.lang.IllegalArgumentException if either the String is
- * badly formatted or if this kind of property can't be expressed
- * as text.
- * @param text The string to be parsed.
- */
- public void setAsText(String text)
- throws java.lang.IllegalArgumentException
- {
- //Search for matching tag
- for (int i = 0;i < m_Tags.length;i++)
- {
- TaggedInt currTag = m_Tags[i];
-
- if (currTag.getTag().equals(text))
- {
- setValueAsInteger(new Integer(currTag.getValue()));
-
- return;
- }
- }
-
- //Call super - it throws
- super.setAsText(text);
- }
-
- protected void setValueAsInteger(Integer newValue)
- {
- setValue(newValue);
- }
-
- /**
- * This method is intended for use when generating Java code to set
- * the value of the property. It should return a fragment of Java code
- * that can be used to initialize a variable with the current property
- * value.
- * <p>
- * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
- *
- * @return A fragment of Java code representing an initializer for the
- * current value.
- */
- public String getJavaInitializationString()
- {
- return getValueAsString(false);
- }
-
- protected Integer getValueAsInteger()
- {
- return (Integer)getValue();
- }
-
- protected String getValueAsString(boolean tag)
- {
- Integer value = getValueAsInteger();
-
- if (value != null)
- {
- int intValue = value.intValue();
-
- //Search for matching tag
- for (int i = 0;i < m_Tags.length;i++)
- {
- TaggedInt currTag = m_Tags[i];
- if (currTag.getValue() == intValue)
- {
- return tag ? currTag.getTag() : currTag.getInitializationString();
- }
- }
- }
-
- return "";
- }
-
- protected TaggedInt[] m_Tags;
- }
-